package com.sgb.kjwl.view.ui.activity.self;

import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;

import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityInvoiceAddBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.link.entity.InvoiceManagementEntity;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.InvoiceAddModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;


import javax.inject.Inject;

/**
 * 新增编辑发票
 */
public class InvoiceAddActivity extends AppActivity {

    @Inject
    InvoiceAddModel viewModel;
    private ActivityInvoiceAddBinding binding;
    private String id;
    private String companyNo;
    private boolean isNewAddress;

    public static void start(Context context, String id, String companyNo) {
        Intent intent = new Intent(context, InvoiceAddActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("companyNo", companyNo);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent)
                .selfModule(new SelfModule(this)).build().inject(this);
    }


    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(InvoiceAddActivity.this, R.layout.activity_invoice_add);
        binding.setViewModel(viewModel);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.rightView.setVisibility(View.GONE);
        binding.include.rightView.setText("保存");
        binding.include.rightView.setOnClickListener(this);
        binding.itemPhone.setInputType(InputType.TYPE_CLASS_NUMBER);
        binding.itemBankNo.setInputType(InputType.TYPE_CLASS_NUMBER);
        binding.itemBankNumber.setInputType(InputType.TYPE_CLASS_NUMBER);
        binding.itvCName.setNoSymbol();
        binding.itvCreditCode.setNumberAndLetter("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
//        binding.itvAddress.setEnable(false);
        binding.itemBank.setChinese();
        binding.tvCommit.setOnClickListener(this);
        binding.tvDelete.setOnClickListener(this);
    }

    @Override
    protected void init() {
        id = getIntent().getStringExtra("id");
        companyNo = getIntent().getStringExtra("companyNo");
        isNewAddress = TextUtils.isEmpty(id);
        if (isNewAddress) {
            binding.include.titleView.setText("新增发票");
            binding.tvDelete.setVisibility(View.GONE);
        } else {
            binding.include.titleView.setText("编辑发票");
            viewModel.billDetail(id);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_commit:
                if (TextUtils.isEmpty(binding.itvCName.getContent().trim())) {
                    MToast.showToast(InvoiceAddActivity.this, "请输入单位名称");
                    return;
                } else if (TextUtils.isEmpty(binding.itvCreditCode.getContent().trim())) {
                    MToast.showToast(InvoiceAddActivity.this, "请输入税号");
                    return;
                } else if (TextUtils.isEmpty(binding.itvAddress.getContent().trim())) {
                    MToast.showToast(InvoiceAddActivity.this, "请输入企业地址");
                    return;
                } else if (TextUtils.isEmpty(binding.itemPhone.getContent().trim())) {
                    MToast.showToast(InvoiceAddActivity.this, "请输入企业电话");
                    return;
                } else if (TextUtils.isEmpty(binding.itemBank.getContent().trim())) {
                    MToast.showToast(InvoiceAddActivity.this, "请输入开户银行");
                    return;
                } else if (binding.itemBank.getContent().trim().length() < 4) {
                    MToast.showToast(InvoiceAddActivity.this, "请输入正确开户银行");
                    return;
                } else if (TextUtils.isEmpty(binding.itemBankNo.getContent().trim())) {
                    MToast.showToast(InvoiceAddActivity.this, "请输入银行账号");
                    return;
                } else if (binding.itemBankNo.getContent().trim().length() < 6) {
                    MToast.showToast(InvoiceAddActivity.this, "请输入正确银行账号");
                    return;
                } else if (TextUtils.isEmpty(binding.itemBankNumber.getContent().trim())) {
                    MToast.showToast(InvoiceAddActivity.this, "请输入开户行行号");
                    return;
                }
                if (binding.switchDefault.isChecked()) {
                    viewModel.invoiceManagementEntity.get().setIsDefault(1);
                } else {
                    viewModel.invoiceManagementEntity.get().setIsDefault(0);
                }
                viewModel.invoiceManagementEntity.get().setCompanyNo(companyNo);
                viewModel.addBill();
                break;
            case R.id.tv_delete:
                showDialog(viewModel.invoiceManagementEntity.get().getId());
                break;
        }
    }

    public void setData(InvoiceManagementEntity data) {
        if (data.getIsDefault() == 1) {
            binding.switchDefault.setChecked(true);
        } else {
            binding.switchDefault.setChecked(false);
        }
    }

    private void showDialog(String id) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失

        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("删除开票信息");
        tv_title.setVisibility(View.VISIBLE);
        tv_message.setText("是否删除开票信息");
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //请求接口
//                deletePerson.put("id", id);
                viewModel.deleteBill(id);
                roundCornerDialog.dismiss();
            }
        });
        //取消
        tv_logout_cancel.setTextColor(getResources().getColor(R.color.color_tab_one));
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
}
